﻿public class NetHandler_UpdateApply : GameServerMessageHandler
{
    public const string Key = "UpdateApply";

    public static NetHandler_UpdateApply NewHandler()
    {
        return new NetHandler_UpdateApply();
    }

    protected override ISerialize NewData() => new UpdateApplyRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        UpdateApplyRequestData requestData = _data as UpdateApplyRequestData;

        GameDebug.Log($"用户{client.userID} 更新申请:{requestData?.apply}");

        UserServer user = Game.instance.GetUser(client.userID);

        ResponseData response = new ResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                var apply = requestData.apply;
                user.data.UpdateApply(requestData.apply);

                long opponent = user.data.id == apply.targetUserId ? apply.sourceUserId : apply.targetUserId;

                if (requestData.apply.result == Apply.ApplyResult_Suc)
                {
                    GameDebug.Log($"{apply.targetUserId} 同意了{apply.sourceUserId}的申请");
                    user.data.AddFriend(opponent);

                    users.UpdateFriends(user.data.id, user.data.friends);
                }

                UserServer opponentUser = Game.instance.GetUser(opponent);
                if(opponentUser != null)
                {
                    opponentUser.data.UpdateApply(requestData.apply);
                    opponentUser.data.AddFriend(user.data.id);

                    GameDebug.Log($"给{opponent} 发送申请结果事件");
                    NetEvent_UpdateApply netEvent_UpdateApply = new NetEvent_UpdateApply();
                    netEvent_UpdateApply.applyData = new UpdateApplyData();
                    netEvent_UpdateApply.applyData.apply = apply;

                    netEvent_UpdateApply.SendToRemote(opponentUser.client);

                    users.UpdateFriends(opponent, opponentUser.data.friends);
                }
                else
                {
                    users opponentUserTable = UserServer.FindInUserTable(opponent);
                    var friendList = users.String2LongList(opponentUserTable.friends);
                    friendList.Add(user.data.id);
                    users.UpdateFriends(opponent, friendList);
                }

                int ret = applys.SetApply(apply.sourceUserId, /*apply.applyId,*/ apply.targetUserId, apply.result,
                    apply.msg, apply.rejectMsg, apply.applyTime, apply.viewTime, apply.resultTime);

                GameDebug.Log($"{apply.sourceUserId} 与 {apply.targetUserId} 更新申请");

                if (ret >= 0)
                    response.code = 0;
                else
                    response.code = -1;
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
